package com.speedchina.doormanager.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.speedchina.doormanager.dao.DoorPlateDao;
import com.speedchina.doormanager.dao.DoorPlateLogDao;
import com.speedchina.doormanager.dao.TaskDao;
import com.speedchina.doormanager.domain.DoorPlate;
import com.speedchina.doormanager.domain.DoorPlateLog;
import com.speedchina.doormanager.domain.Task;
import com.speedchina.doormanager.domain.TaskDoorPlate;
import com.speedchina.doormanager.dto.DoorPlateTaskDTO;
import com.speedchina.doormanager.enums.DoorPlateStatusEnum;
import com.speedchina.doormanager.enums.ResultEnum;
import com.speedchina.doormanager.enums.TaskStatusEnum;
import com.speedchina.doormanager.enums.TaskTypeEnum;
import com.speedchina.doormanager.exception.GlobalException;
import com.speedchina.doormanager.service.DoorPlateLogService;
import com.speedchina.doormanager.service.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


@Service
public class DoorPlateLogServiceImpl implements DoorPlateLogService {

    @Autowired
    private DoorPlateLogDao doorPlateLogDao;

    @Autowired
    private DoorPlateDao doorPlateDao;

    @Autowired
    private TaskService taskService;

    @Autowired
    private TaskDao taskDao;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveDoorPlateLog(DoorPlateLog doorPlateLog, String type) {
        doorPlateLog.setAddTime(new Date());
        doorPlateLog.setSortId("99");
        doorPlateLog.setTaskType(type);
        // 如果类型是安装，需要判断是否当前有安装日志
        if (type.equals(TaskTypeEnum.ANZHUANG.getStatus())){
            List<DoorPlateLog> doorPlateLogList = doorPlateLogDao.selectList(new EntityWrapper<DoorPlateLog>()
                    .eq("task_type", TaskTypeEnum.ANZHUANG.getStatus())
                    .eq("doorPlate_id", doorPlateLog.getDoorPlateId()));
            if (!doorPlateLogList.isEmpty()) {
                throw new GlobalException(ResultEnum.THIS_DOOR_PLATE_ALREADY_COMPETE.getCode(),
                        ResultEnum.THIS_DOOR_PLATE_ALREADY_COMPETE.getMessage() + type);
            }
        }

        // 插入操作日志信息
        int success = doorPlateLogDao.insert(doorPlateLog);
        if (success != 1) {
            throw new GlobalException(ResultEnum.SAVE_LOG_ERROR);
        }

        DoorPlate doorPlate = new DoorPlate();
        // 如果任务类型为安装
        if (TaskTypeEnum.ANZHUANG.getStatus().equals(type)) {
            doorPlate.setStatus(DoorPlateStatusEnum.YIANZHUANG.getStatus());
        } else if (TaskTypeEnum.XUNJIAN.getStatus().equals(type)) {
            doorPlate.setStatus(DoorPlateStatusEnum.YIXUNJIAN.getStatus());
        } else if (TaskTypeEnum.WEIHU.getStatus().equals(type)) {
            doorPlate.setStatus(DoorPlateStatusEnum.YIWEIHU.getStatus());
        }
        doorPlateDao.update(doorPlate, new EntityWrapper<DoorPlate>().eq("id", doorPlateLog.getDoorPlateId()));


        // 更新任务状态
        DoorPlateTaskDTO doorPlateTaskDTO = taskService.findTaskDTOById(doorPlateLog.getTaskId());
        Task task = new Task();
        // 如果完成的数量大于0，并且小于总数量
        if (doorPlateTaskDTO.getFinishNumber() > 0 &&
                doorPlateTaskDTO.getFinishNumber() < doorPlateTaskDTO.getTotalNumber()) {
            task.setStatus(TaskStatusEnum.JINXINGZHONG.getStatus());
            taskDao.update(task, new EntityWrapper<Task>().eq("id", doorPlateLog.getTaskId()));
        }
        // 如果完成的数量等于总数量
        if (doorPlateTaskDTO.getFinishNumber().equals(doorPlateTaskDTO.getTotalNumber())) {
            task.setStatus(TaskStatusEnum.YIWANCHENG.getStatus());
            task.setShijiTime(new Date());
            taskDao.update(task, new EntityWrapper<Task>().eq("id", doorPlateLog.getTaskId()));
        }
        return true;
    }

}
